package com.yjhb.android.sbz.dialog;

import android.app.Activity;
import android.view.View;

import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.entity.UserInfo;
import com.yjhb.android.sbz.mvp.settinginfo.SettingInfoPresenter;
import com.yjhb.android.sbz.mvp.settinginfo.SettingInfoView;
import com.yjhb.android.sbz.tool.wx.WxLogin;
import com.jojo.android.zxlib.tool.ZxToast;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 创建者 zx
 * 创建时间 2021/2/4 10:35
 */
public class BindWxDialog extends TipsDialog implements SettingInfoView {
    private final String TAG = "BindWxDialog";

    private SettingInfoPresenter presenter;

    public BindWxDialog(Activity context) {
        super(context);
        init();
    }

    private void init() {
        setTitle("提示");
        setContent("您还没有绑定微信，是否绑定？");
        setSubmitText("绑定");
        presenter = new SettingInfoPresenter(this);

    }

    @Override
    protected void onStart() {
        EventBus.getDefault().register(this);
        super.onStart();
    }

    @Override
    public void onClick(View v) {
        if (v == submit_tv) {
            WxLogin.login();
        } else {
            dismiss();
        }
        super.onClick(v);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMsg(EventMsg eventMsg) {
        if (eventMsg.msg == EventMsg.USER_WX_RESULT) {
            SendAuth.Resp resp = (SendAuth.Resp) eventMsg.object;
            if (resp != null && resp.errCode == BaseResp.ErrCode.ERR_OK) {
                presenter.bindWx(resp.code);
            }
        }
    }

    @Override
    protected void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
    }

    @Override
    public void onUserInfo(UserInfo userInfo) {

    }

    @Override
    public void onOk() {
        ZxToast.showSuccessCenterToast("绑定成功！");
        dismiss();
        EventBus.getDefault().post(new EventMsg(EventMsg.REQUEST_USER_UPDATE, null));
    }

    @Override
    public void onWait(String tips) {

    }

    @Override
    public void onZfbChangeSuccess() {

    }

    @Override
    public void onFailure(int code, String msg) {
        ZxToast.showINFOBottomToast(msg);
    }
}
